package com.youhong.teethcare;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.youhong.teethcare.adapter.BrushRecordAdapter;
import com.youhong.teethcare.history.HistoryDataFragment;
import com.youhong.teethcare.history.NoDataFragment;
import com.youhong.teethcare.history.RealtimeDataFragment;
import com.youhong.teethcare.javabeans.BrushingRecord;
import com.youhong.teethcare.utils.DBHelper;
import com.youhong.teethcare.utils.Util;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryFragment extends Fragment implements View.OnClickListener {
    private ImageView iv_recordDate;

    public HistoryFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_recode_list, container, false);

        selected_date = Calendar.getInstance();
        getViews(view);
        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQ_DATE_PICKER && resultCode == Activity.RESULT_OK) {

            Calendar c = (Calendar) data.getSerializableExtra("date");
            selected_date = (Calendar) c.clone();

            Calendar today = Calendar.getInstance();

            if (c.get(Calendar.YEAR) == today.get(Calendar.YEAR) &&
                    c.get(Calendar.MONTH) == today.get(Calendar.MONTH) &&
                    c.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)
                    ) {
                tv_date.setText(R.string.strings16);

            } else {

                tv_date.setText(Util.FormDateTimeString2(c));
            }
            displayData(c);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    TextView tv_date;


    private void getViews(View view) {
        tv_date = view.findViewById(R.id.tvDate);
        recyclerViewList = view.findViewById(R.id.RecyclerView_Record);
        iv_recordDate = view.findViewById(R.id.iv_recordDate);
        iv_recordDate.setOnClickListener(this);
        initRecordAdapter();
        displayData(Calendar.getInstance());

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (!hidden) {
            Calendar today = Calendar.getInstance();

            if (selected_date.get(Calendar.YEAR) == today.get(Calendar.YEAR) &&
                    selected_date.get(Calendar.MONTH) == today.get(Calendar.MONTH) &&
                    selected_date.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)
                    ) {
                tv_date.setText(R.string.strings16);

            } else {

                tv_date.setText(Util.FormDateTimeString2(selected_date));

            }

            displayData(selected_date);
        }
    }
    RecyclerView recyclerViewList;
    BrushRecordAdapter brushRecordAdapter;
    void initRecordAdapter(){
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
         brushRecordAdapter=new BrushRecordAdapter(new ArrayList<>());
         brushRecordAdapter.setItemClickListener(new BrushRecordAdapter.OnItemClickListener() {
             @Override
             public void onItemClick(@NotNull BrushingRecord record) {
                 Intent intent=new Intent(getContext(),BrushAreaShowActivity.class);
                 intent.putExtra("record",record);
                 startActivity(intent);
             }
         });
        recyclerViewList.setAdapter(brushRecordAdapter);
        recyclerViewList.setLayoutManager(linearLayoutManager);

    }
    void updateData(List<BrushingRecord> list){
        brushRecordAdapter.updateData(list);
    }
    Fragment previous_fragment;

    public void setSelected_date(Calendar selected_date) {
        this.selected_date = selected_date;
    }

    Calendar selected_date;

    private void displayData(Calendar c) {

        FragmentTransaction ft = getChildFragmentManager().beginTransaction();

        List<BrushingRecord> list =
                DBHelper.getDbHelper(getContext()).queryBrushRecordByDay(c,1);
        updateData(list);

    }

    private static final int REQ_DATE_PICKER = 0x1;

    @Override
    public void onClick(View v) {

        if (v == iv_recordDate) {

            Intent intent = new Intent(getContext(), DatePickerActivity.class);
            intent.putExtra("date", selected_date);
            startActivityForResult(intent, REQ_DATE_PICKER);

        }

    }
}